package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;


public class Telefono {

	public static int lastTelIdDb=0;
	private int tel_id = 0;
	private String num = "";

	//costruttore telefono per nuovo inserimento
	public Telefono() {
		//set_LastTelIDfromDB(get_LastTelIDfromDB());
		/*this.tel_id = Telefono.lastTelIdDb;
		Telefono.lastTelIdDb++;*/
	}

	//costruttore telefono con dati
	public Telefono(int tel_id, String num){
		this.setTel_id(tel_id);
		this.setNum(num);
		/*if (this.tel_id>Telefono.lastTelIdDb)
			Telefono.lastTelIdDb = this.tel_id;*/
	}

	/*public int get_LastTelIdDb() {
		return lastTelIdDb;
	}*/

	public void set_LastTelIDfromDB() {
		Telefono.lastTelIdDb++;
	}

	//estrazione risorse dal db per
	public int get_LastTelIDfromDB() {
		ResultSet result = Database.getQueryResult("SELECT tel_id FROM telefono");
		int id=0;
		try {
			while(result.next())
				id=result.getInt("tel_id");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Telefono.lastTelIdDb = id+1;
		return id+1;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public int getTel_id() {
		//tel_id = get_LastTelIDfromDB();
		return tel_id;
	}

	public void setTel_id(int tel_id) {
		this.tel_id = tel_id;
	}

	public static Vector<Telefono> accTel(int acc_id){
		Vector<Telefono> tel = new Vector<Telefono>();
		try {
			ResultSet resultTel = Database.getQueryResult("SELECT * FROM telefono WHERE acc_id="+acc_id);
			while (resultTel.next())
				tel.add(new Telefono(resultTel.getInt("tel_id"),resultTel.getString("num")));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tel;
	}
}
